package com.example.administrator.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.administrator.myapplication.Bean.LoginBean;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class RegisterActivity extends AppCompatActivity {
    private EditText Ename;
    private EditText Eusername;
    private EditText Eemail;
    private EditText Etelephone;
    private EditText Erepassword;
    private EditText Epassword;
    private Button registBtn;
    private Spinner departmentSp;
    private Spinner majorSp;
    String majorStr,departmentStr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        initView();
        initEvent();
    }
    private void initView(){
        Ename = (EditText) findViewById(R.id.regist_name_ed);
        Eusername = (EditText)findViewById(R.id.regist_username_ed);
        Eemail = (EditText)findViewById(R.id.regist_email_ed);
        Etelephone =(EditText) findViewById(R.id.regist_telephone_ed);
        Erepassword = (EditText)findViewById(R.id.regist_repassword_ed);
        Epassword =(EditText) findViewById(R.id.regist_password_ed);
        departmentSp =(Spinner) findViewById(R.id.regist_department_sp);
        majorSp =(Spinner) findViewById(R.id.regist_major_sp);
        registBtn =(Button) findViewById(R.id.regist_btn);

    }
    private void initEvent(){
        //获取部门下拉选择框数据
        departmentSp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                String[] departmentArray = getResources().getStringArray(R.array.department);
                departmentStr = departmentArray[i].toString();
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        majorSp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                String[] majorArray = getResources().getStringArray(R.array.majorarray);
                majorStr = majorArray[i].toString();
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        registBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(RegisterActivity.this, "123", Toast.LENGTH_SHORT).show();
                initData();
            }
        });

    }
    private void initData(){
        String name = Ename.getText().toString();
        String username = Eusername.getText().toString();
        String email = Eemail.getText().toString();
        String telephone = Etelephone.getText().toString();
        String password = Epassword.getText().toString();
        String repassword = Erepassword.getText().toString();
        if (password.equals(repassword)){
            JSONObject jsonObject = new JSONObject();
            try {
                jsonObject.put("name", name);

                jsonObject.put("email", email);
                jsonObject.put("telephone", telephone);
                jsonObject.put("password", password);
                jsonObject.put("department", departmentStr);
                jsonObject.put("major", majorStr);
                jsonObject.put("role", "学生");
            }catch (JSONException e){
                e.printStackTrace();
            }
            //创建网络请求队列
            RequestQueue requestQueue = Volley.newRequestQueue(RegisterActivity.this);

            //发起网络请求
            String url = "http://www.xhjjyst.com/freshmenapp/user/regist";
            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url,
                    jsonObject, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject jsonObject) {


                        // startActivity(new Intent(UserLoginActivity.this,HomeActivity.class));
                        Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                        finish();

                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    Toast.makeText(RegisterActivity.this,"请求失败",Toast.LENGTH_LONG).show();
                }
            });

            //讲请求内容嵌入队列中
            requestQueue.add(jsonObjectRequest);




        }else {
            Toast.makeText(RegisterActivity.this, "两次密码不一样", Toast.LENGTH_SHORT).show();
        }

    }
}